package com.example.demo.controller;

import com.example.demo.aspect.annotation.ThankCustomer;
import com.example.demo.entity.Food;
import com.example.demo.entity.Order;
import com.example.demo.service.FreshKitchenService;
import com.example.demo.service.FoodManagerService;
import com.example.demo.service.KitchenService;
import com.example.demo.service.PoultryKitchenService;
import com.example.demo.utils.Waiter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cc
 * @date 2018/9/19 12:23
 */
@RestController
@RequestMapping("/restaurant")
public class HelloRestaurant {
    private final FoodManagerService foodManagerService;
    private final KitchenService kitchenService;
    private final FreshKitchenService freshKitchenService;
    private final PoultryKitchenService poultryKitchenService;

    @Autowired
    public HelloRestaurant(FoodManagerService foodManagerService, KitchenService kitchenService, FreshKitchenService freshKitchenService, PoultryKitchenService poultryKitchenService) {
        this.foodManagerService = foodManagerService;
        this.kitchenService = kitchenService;
        this.freshKitchenService = freshKitchenService;
        this.poultryKitchenService = poultryKitchenService;
    }

    @GetMapping("/hello")
    public String hello() {
        return "hello restaurant!";
    }

    @PostMapping("/purchaseFood")
    public String purchaseFood(@RequestBody JSONObject jsonObject) {
        Food food = (Food) JSONObject.toBean(jsonObject, Food.class);

        foodManagerService.storeFood(food);
        return "完成采购" + food.getFoodName();
    }

    @PostMapping("/orderDishes")
    public Map<Object, Object> order(@RequestBody JSONObject jsonObject) {
        Order order = (Order) JSONObject.toBean(jsonObject, Order.class);
        List orderFoods = JSONArray.toList(jsonObject.getJSONArray("orderFoods"), new Food(), new JsonConfig());
        List<Food> cookedFoods = new ArrayList<>();
        for (Object orderFood1 : orderFoods) {
            Food orderFood = (Food) orderFood1;
            switch (orderFood.getFoodName()) {
                case "鸡":
                case "鸭":
                    Food cookedPoultry = poultryKitchenService.cookPoultry(orderFood);
                    if (cookedPoultry != null) {
                        cookedFoods.add(cookedPoultry);
                    }
                    break;
                case "鱼":
                case "虾":
                    // TODO: 2018/9/22
                    break;
                default:
                    break;
            }
        }
        order.setOrderFoods(cookedFoods);
        return Waiter.success(order, null);
    }
}
